package zachg.survival;

import com.threerings.media.sprite.Sprite;
import com.threerings.util.Name;

import java.awt.Color;
import java.awt.Graphics2D;

import zachg.survival.SurvivalObject;

/**
 * The Class GameCharacterSprite.
 * Extends the sprite class to include a character, 
 * coloring the sprite as appropriate based on that character
 * 
 */
public class GameCharacterSprite extends Sprite {

	/**
	 * Creates a piece sprite to display the supplied game piece.
	 */
	public GameCharacterSprite (SurvivalObject.GameCharacter gameCharacter)
	{
		super(SIZE, SIZE);
		updateGameCharacter(gameCharacter);
	}
	
	/**
	 * Called when the piece we are displaying has been updated.
	 */
	public void updateGameCharacter (SurvivalObject.GameCharacter gameCharacter)
	{
		// keep track of our piece
		_character = gameCharacter;
		
		// set our location based on the location of the piece
		setLocation(_character.x * SIZE, _character.y * SIZE);
		
		// force a redraw in case our color changed but not our location
		invalidate();
	}
	
	@Override // from Sprite
	public void paint (Graphics2D gfx)
	{
		// set our color depending on the player that owns this piece
		
		if(_character.ownerType == SurvivalObject.PLAYER_ID){
			//this is a player character, give it one of the fixed player colors 
			gfx.setColor(SurvivalObject.PlayerColors[_character.playerIndex]);
		} else if(_character.ownerType == SurvivalObject.ENEMY_ID){
			//this is creeper or a placeholder
			if(_character.playerOwner != null){
				if(_character.playerOwner.compareTo(new Name(SurvivalObject.CREEPER_NAME)) > 0){
					//placeholder character
					gfx.setColor(Color.green);
				} else {
					//creeper character					
					gfx.setColor(Color.red);
				}
			} else {
				gfx.setColor(Color.lightGray);
			}
		} else {
			gfx.setColor(Color.green);
		}
		gfx.fillOval(_bounds.x, _bounds.y, _bounds.width, _bounds.height);
		
		// then outline that oval in black; note: fill... and draw... need to
		// be called with different width and height (see javadoc of Graphics)
		gfx.setColor(Color.black);
		gfx.drawOval(_bounds.x, _bounds.y, _bounds.width-1, _bounds.height-1);
	}
	
	/** The dimensions of our sprite in pixels. */
	public static final int SIZE = 12;
	
	protected SurvivalObject.GameCharacter _character;
}